const User = require("../../../models/user/userModel.js");
const { buildErrObject } = require("../../../utils/buildErrObject");
const registerUser = function (req = {}) {
	return new Promise(async (resolve, reject) => {
		let userInfo = await User.findOne({
			user_name: req.user_name,
			is_delete: false,
		});
		const user = new User({
			user_name: req.user_name,
			nick_name: "半岛·" + Math.random().toString(36).substr(2),
			mobile: req.mobile,
			password: req.password,
		});
		if (userInfo === null) {
			user.save((err, item) => {
				if (err) {
					reject(buildErrObject(422, err.message));
				}
				resolve(item);
			});
			return;
		}
		return reject(buildErrObject(422, "用户已存在"));
	});
};

module.exports = { registerUser };
